package com.practice.Java中HttpClient如何使用;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-01-23 11:07
 * @Version 1.0
 * @description:
 **/
public class NetHttpURLConnectionDemo {
    public static void main(String[] args) throws IOException {
        String target = "https://www.baidu.com/?tn=sitehao123_15";
        // 发送HTTP请求
        URL url = new URL(target);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        //connection.setRequestMethod("POST");
        connection.connect();
        BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream())) ;

        // 获取网页源代码
        String htmlPage="";
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            htmlPage += inputLine;
        }
        System.out.println("htmlPage:>>>" + htmlPage);
        in.close();
        connection.disconnect();
        System.out.println("-------------------------------------------");

        // 解析HTML(Jsoup是一个HTML解析器，可解析成Document)
        Document document = Jsoup.parse(htmlPage);
        // 数据提取示例
        Elements titles = document.select("h1,h2,h3");
        //titles.forEach(title -> System.out.println("tttt>>>"+title.text()));
        for(Element e :titles){
            System.out.println("wwww>>>"+e.text());
        }

    }
    public static void main1(String[] args) throws Exception {
        URL url = new URL("https://example.com");

        // 打开连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();

        // 检查连接状态码
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 获取响应流
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String inputLine;
            StringBuilder content = new StringBuilder();

            // 读取并打印内容
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }

            in.close();

            // 打印网页内容
            System.out.println(content.toString());
        } else {
            System.out.println("Failed with response code: " + responseCode);
        }

        conn.disconnect();
    }
}